package com.jorado.etl.service;

import com.jorado.core.Result;
import com.jorado.etl.repository.SolrPositionRepository;
import com.jorado.event.EventClient;
import com.jorado.event.dubbo.util.SpringUtils;
import com.jorado.search.core.service.BaseExport;
import com.jorado.search.core.service.impl.SolrExporter;
import com.jorado.search.core.util.SolrUtils;
import com.jorado.search.solr.client.SolrjOption;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.common.SolrDocument;
import org.springframework.context.annotation.Description;

import java.util.List;

@Description("Solr数据同步到数据库")
public final class SolrToDBService extends BaseExport<SolrDocument> {

    private int total;
    private int waitTime;
    private boolean clearBefore;
    private SolrPositionRepository repository;

    public SolrToDBService(SolrjOption positionSolr, int waitTime) {
        this(positionSolr, waitTime, false);
    }

    public SolrToDBService(SolrjOption positionSolr, int waitTime, boolean clearBefore) {
        super(new SolrExporter(positionSolr, getSolrQuery()));
        this.waitTime = waitTime;
        this.repository = SpringUtils.getBean(SolrPositionRepository.class);
        this.clearBefore = clearBefore;
        this.setAfterFilter(this::after);
    }

    protected Result before() {
        if (this.clearBefore) {
            repository.clear();
        }
        return Result.OK;
    }

    @Override
    protected void export(List<SolrDocument> dataList) {

        total += dataList.size();

        String id;
        String jobTitle;
        String jobType;
        String jobTypeName;
        String jobDescription;
        String industryId;
        String industryName;
        String companyId;
        String companyName;
        String companyShortName;

        for (SolrDocument doc : dataList) {

            id = SolrUtils.getDocValue(doc, "ID");
            jobTitle = SolrUtils.getDocValue(doc, "JOB_TITLE");
            jobType = SolrUtils.getDocValue(doc, "JOB_TYPE");
            jobTypeName = SolrUtils.getDocValue(doc, "JOB_TYPE_NAME");
            jobDescription = SolrUtils.getDocValue(doc, "JOB_DESC");
            industryId = SolrUtils.getDocValue(doc, "INDUSTRY_ID");
            industryName = SolrUtils.getDocValue(doc, "INDUSTRY_NAME");
            companyId = SolrUtils.getDocValue(doc, "COMPANY_ID");
            companyName = SolrUtils.getDocValue(doc, "COMPANY_NAME");
            companyShortName = SolrUtils.getDocValue(doc, "COMPANY_NAME_SHORT");
            repository.insert(id, jobTitle, jobType, jobTypeName, jobDescription, industryId, industryName, companyId, companyName, companyShortName);
        }

        try {
            Thread.sleep(waitTime);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected Result after(Result result) {

        EventClient.getDefault().asyncSubmitLog(String.format("共同步solr职位:[%d]个", total));

        return Result.OK;
    }

    private static SolrQuery getSolrQuery() {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery("*:*");
        solrQuery.setFields("ID,JOB_TITLE,JOB_TYPE,JOB_TYPE_NAME,JOB_DESC,INDUSTRY_ID,INDUSTRY_NAME,COMPANY_ID,COMPANY_NAME,COMPANY_NAME_SHORT");
        solrQuery.setSort("DATE_CREATED", SolrQuery.ORDER.asc);
        return solrQuery;
    }
}
